<template>
  <div>
    <van-pull-refresh v-model="isLoading" @refresh="onRefresh">
      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        @load="onLoad"

      >
        <artItem
          v-for="item in artlist"
          :key="item.id"
          :itemObj="item"
           @removeItem = "removeItemFn"
        ></artItem>
      </van-list>
    </van-pull-refresh>

  </div>
</template>

<script>

import { getArtListAPI } from '@/api/homeAPI'
// 引入artItem组件
import artItem from '../artItem/artItem.vue'

export default {
  components: {
    artItem
  },
  data () {
    return {
      // 时间戳
      timestamp: Date.now(),
      artlist: [],
      loading: false,
      // 是否已加载完成，加载完成后不再触发load事件
      finished: false,
      isLoading: false

    }
  },
  props: {
    channelId: {
      type: Number,
      required: true
    }
  },
  methods: {
    async initGetartList (isRefesh) {
      const { data: res } = await getArtListAPI({
        channel_id: this.channelId,
        timestamp: this.timestamp
      })
      if (res.message === 'OK') {
        this.timestamp = res.data.pre_timestamp

        if (isRefesh == true) {
          // 下拉刷新
          // isRefesh 是 true代表下拉刷新
          this.artlist = [...res.data.results, ...this.artlist]
          this.loading = false
        } else if (isRefesh == false) {
          // 上拉刷新
          // isRefesh 是 false代表下拉刷新
          this.artlist = [...this.artlist, ...res.data.results]
          this.loading = false
        }
        // console.log(this.artlist);
        if (res.data.pre_timestamp == null) {
          this.finished = true
        }
      }
    },
    onLoad () {
      this.initGetartList(false)
    },
    onRefresh () {
      // console.log("下拉刷新");
      this.initGetartList(true)
      this.isLoading = false
    },
    // 对文章不感兴趣
    removeItemFn (id) {
      // console.log(this.artlist)
      this.artlist = this.artlist.filter(function (item) {
        return item.art_id != id
      })
      if (this.artlist.length < 10) {
        this.initGetartList(true)
      }
    }
  },
  created () {
    this.initGetartList()
  }
}
</script>

<style lang="less" scoped>
</style>
